﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MinhTuan;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
namespace Products
{
   public class ShoppingCart
    {
        #region    Properties

        private Int32 _Order_Id;
        private Int32 _UserId;
        private DateTime _Order_Date;
        private Int32 _Order_Quantity;
        private Double _Order_Total;
        private String _Shipping_Name;
        private String _Shipping_Address;
        private String _Shipping_Tel;
        private String _Shipping_City;
        private String _Shipping_Zipcode;
        private String _Shipping_Country;
        private String _Order_Notice;
        private List<CartItem> _Items;

        public List<CartItem> Items
        {
            get { return _Items; }
            set { _Items = value; }
        }

        public Int32 Id
        {
            get { return _Order_Id; }
            set { _Order_Id = value; }
        }

        public Int32 UserId
        {
            get { return _UserId; }
            set { _UserId = value; }
        }

        public DateTime OrderDate
        {
            get { return _Order_Date; }
            set { _Order_Date = value; }
        }

        public String ShippingName
        {
            get { return _Shipping_Name; }
            set { _Shipping_Name = value; }
        }

        public String ShippingAddress
        {
            get { return _Shipping_Address; }
            set { _Shipping_Address = value; }
        }

        public String ShippingTel
        {
            get { return _Shipping_Tel; }
            set { _Shipping_Tel = value; }
        }

        public String ShippingCity
        {
            get { return _Shipping_City; }
            set { _Shipping_City = value; }
        }

        public String ShippingZipcode
        {
            get { return _Shipping_Zipcode; }
            set { _Shipping_Zipcode = value; }
        }

        public String ShippingCountry
        {
            get { return _Shipping_Country; }
            set { _Shipping_Country = value; }
        }

        public String OrderNotice
        {
            get { return _Order_Notice; }
            set { _Order_Notice = value; }
        }


        #endregion

        #region   Constructor

        public ShoppingCart()
        {
            _Items = new List<CartItem>();
        }

        public ShoppingCart(List<CartItem> ShopCart)
        {
            _Items = ShopCart;
        }

        public ShoppingCart(Int32 Order_Id, Int32 UserId, DateTime Order_Date, Int32 Order_Quantity, Double Order_Total, String Shipping_Name, String Shipping_Address, String Shipping_Tel, String Shipping_City, String Shipping_Zipcode,
        String Shipping_Country, String Order_Notice, List<CartItem> ShopCart)
        {
            _Order_Id = Order_Id;
            _UserId = UserId;
            _Order_Date = Order_Date;
            _Order_Quantity = Order_Quantity;
            _Order_Total = Order_Total;
            _Shipping_Name = Shipping_Name;
            _Shipping_Address = Shipping_Address;
            _Shipping_Tel = Shipping_Tel;
            _Shipping_City = Shipping_City;
            _Shipping_Zipcode = Shipping_Zipcode;
            _Shipping_Country = Shipping_Country;
            _Order_Notice = Order_Notice;
            _Items = ShopCart;
        }

        #endregion

        #region Public Properties

        public int Quantity
        {
            get
            {
                int tempResult = 0;
                foreach (CartItem myCartItem in _Items)
                {
                    tempResult += myCartItem.Quantity;
                }
                return tempResult;
            }
        }

       
        public Decimal Total
        {
            get
            {
                Decimal tempResult =0;
                foreach (CartItem myCartItem in _Items)
                {
                    tempResult += myCartItem.Quantity * myCartItem.Price;
                }
                return tempResult;
            }
        }

        public int Count
        {
            get { return _Items.Count; }
        }

        #endregion

        #region    Method

        public void Add(CartItem myCartItem)
        {
            foreach (CartItem item in _Items)
            {
                if (myCartItem.ItemId == item.ItemId)
                {
                    item.Quantity += 1;
                    return;
                }
            }
            _Items.Add(myCartItem);
        }

        public void Add(Int32 ItemId, Int32 Quantity)
        {
            foreach (CartItem item in _Items)
            {
                if (ItemId == item.ItemId)
                {
                    item.Quantity += Quantity;
                    break;
                }
            }
            _Items.Add(new CartItem(ItemId,Quantity));
        }

        private static void AddCartItem(List<SelectedProduct> myList, SelectedProduct myItem)
        {
            foreach (SelectedProduct item in myList)
            {
                if (myItem.Id == item.Id)
                {
                    item.Quantity += 1;
                    return;
                }
            }
            myList.Add(myItem);
        }

        public static List<SelectedProduct> GetShoppingItems(String SelectItems ,String Lang)
       {
           List<SelectedProduct> ShoppingItems = new List<SelectedProduct>();
           try
           {
               if (SelectItems.Length > 0)
               {
                   SelectItems = SelectItems.Remove(0,1);
                   String[] CurrSelect  =  SelectItems.Split ('-');    
                   String[] ItemInfo ;
                   SelectedProduct myItem;
                   for (Int16 index = 0; index < CurrSelect.Length; index ++)
                   {
                       ItemInfo = CurrSelect[index].Split(',');
                       myItem = new SelectedProduct(Convert.ToInt32(ItemInfo[0]),Convert.ToInt32(ItemInfo[1]));
                       AddCartItem(ShoppingItems, myItem);
                   } 
                   Access.InitAccess();
                   SqlDataReader myReader;
                   for (Int16 index = 0; index < ShoppingItems.Count; index ++)
                   {
                       Access.mySqlCommand.Parameters.Clear();
                       Access.mySqlCommand.CommandText = "Sp_Products_GetSelectedItemDetail";
                       Access.mySqlCommand.Parameters.Add("@Item_Id", SqlDbType.Int).Value = ShoppingItems[index].Id;
                       Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang;
                       myReader = Access.mySqlCommand.ExecuteReader();
                       if (myReader.Read())
                       {
                           ShoppingItems[index].Image = myReader["Product_Image"].ToString();
                           ShoppingItems[index].Name = myReader["Product_Name"].ToString();
                           ShoppingItems[index].Price = Convert.ToInt32(myReader["Product_Price"]);
                           ShoppingItems[index].Unit = myReader["Product_Unit"].ToString();
                           ShoppingItems[index].URL = myReader["Product_Title"].ToString();
                       }
                       myReader.Close();
                   }
               }
               return ShoppingItems;
           }
           catch (Exception)
           {
               return ShoppingItems;
           }
       }

        public static String UpdateShoppingcart( List<SelectedProduct> myShoppingCart)
        {
            String Value = "";
            foreach (SelectedProduct myItem in myShoppingCart)
            {
                Value += "-" + myItem.Id.ToString() + "," + myItem.Quantity.ToString();
            }
            return Value;
        }

        //public static void GetCartItems(ShoppingCart myShoppingCart, ListView Container)
        //{
        //   try
        //    { 
        //        myShoppingCart.Items.Clear();
        //        String[] cartinfo;
        //        CartItem myCartItem = new CartItem();
        //        HiddenField hdinfo ;
        //        foreach (ListViewItem myItem in Container.Items)
        //        {
        //            if (myItem.ItemType == ListViewItemType.DataItem)
        //            {
        //                hdinfo = (HiddenField)myItem.FindControl("hdinfo");
        //               if (hdinfo != null)
        //                {
        //                    cartinfo = hdinfo.Value.ToString().Split(',');
        //                    myCartItem = new CartItem(Convert.ToInt32(cartinfo[0]), cartinfo[1], Convert.ToInt32(cartinfo[2]), Convert.ToDecimal(cartinfo[3]), cartinfo[4]);
        //                    myShoppingCart.Items.Add(myCartItem);
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        #endregion

        #region Utilities

        public static Int32 AddNewOrder(ShoppingCart myShoppingCart)
        {
            try
            {	        
        		Access.InitAccess();
                Int16 Result = 0; int OrderId = 0;
                Access.mySqlCommand.Parameters.Clear();
                Access.mySqlCommand.CommandText = "Sp_ShoppingCart_AddOrder";
                Access.mySqlCommand.Parameters.Add("@Member_Id", SqlDbType.Int).Value = myShoppingCart.UserId;
                Access.mySqlCommand.Parameters.Add("@Order_Quantity", SqlDbType.Int).Value = myShoppingCart.Quantity;
                Access.mySqlCommand.Parameters.Add("@Order_Total", SqlDbType.Int).Value = myShoppingCart.Total;
                Access.mySqlCommand.Parameters.Add("@Order_Date", SqlDbType.DateTime).Value = myShoppingCart.OrderDate;
                Access.mySqlCommand.Parameters.Add("@Shipping_Name", SqlDbType.NVarChar).Value = myShoppingCart.ShippingName;
                Access.mySqlCommand.Parameters.Add("@Shipping_Address", SqlDbType.NVarChar).Value = myShoppingCart.ShippingAddress;
                Access.mySqlCommand.Parameters.Add("@Shipping_Tel", SqlDbType.NVarChar).Value = myShoppingCart.ShippingTel;
                Access.mySqlCommand.Parameters.Add("@Shipping_City", SqlDbType.NVarChar).Value = myShoppingCart.ShippingCity;
                Access.mySqlCommand.Parameters.Add("@Shipping_Notice", SqlDbType.NVarChar).Value = myShoppingCart.OrderNotice;
                OrderId = Convert.ToInt32(Access.mySqlCommand.ExecuteScalar());
                if (OrderId <= 0)
                {
                    Access.ClosedAccess();
                    return -1;
                }
                else
                {
                    foreach (CartItem myItem in myShoppingCart.Items)
                    {
                        Access.mySqlCommand.Parameters.Clear();
                        Access.mySqlCommand.CommandText = "Sp_ShoppingCart_AddOrderItem";
                        Access.mySqlCommand.Parameters.Add("@Orders_Id", SqlDbType.Int).Value = OrderId;
                        Access.mySqlCommand.Parameters.Add("@Products_Id", SqlDbType.Int).Value = myItem.ItemId;
                        Access.mySqlCommand.Parameters.Add("@Products_Name", SqlDbType.NVarChar).Value = myItem.Name;
                        Access.mySqlCommand.Parameters.Add("@Unit", SqlDbType.NVarChar).Value = myItem.Unit;
                        Access.mySqlCommand.Parameters.Add("@Quantity", SqlDbType.Int).Value = myItem.Quantity;
                        Access.mySqlCommand.Parameters.Add("@Price", SqlDbType.Int).Value = myItem.Price;
                        Result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                        if (Result != 1)
                        {
                           Access.mySqlCommand.Parameters.Clear();
                           Access.mySqlCommand.CommandText = "Sp_ShoppingCart_DeleteOrder";
                           Access.mySqlCommand.Parameters.Add("@Order_Id", SqlDbType.Int).Value = myShoppingCart.Id;
                           Access.mySqlCommand.ExecuteNonQuery();
                           return -1;
                        }
                    }
                    Access.ClosedAccess();
                    return OrderId;
              }
            }
            catch (Exception)
            {
                Access.ClosedAccess();
                return -1;
            }
        }
        public static Int16 Addorder(ShoppingCart Cart)
        {
            Int16 Result = 0;
            //try
            //{
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_ShoppingCart_AddOrder";
                Access.mySqlCommand.Parameters.Add("@Member_Id", SqlDbType.Int).Value = Cart.UserId;
                Access.mySqlCommand.Parameters.Add("@Order_Quantity", SqlDbType.Int).Value = Cart.Quantity;
                Access.mySqlCommand.Parameters.Add("@Order_Total", SqlDbType.Int).Value =Cart.Total;
                Access.mySqlCommand.Parameters.Add("@Order_Date", SqlDbType.DateTime).Value =Cart.OrderDate;
                Access.mySqlCommand.Parameters.Add("@Shipping_Name", SqlDbType.NVarChar).Value =Cart.ShippingName;
                Access.mySqlCommand.Parameters.Add("@Shipping_Address", SqlDbType.NVarChar).Value = Cart.ShippingAddress;
                Access.mySqlCommand.Parameters.Add("@Shipping_Tel", SqlDbType.NVarChar).Value =  Cart.ShippingTel;
                Access.mySqlCommand.Parameters.Add("@Shipping_City", SqlDbType.NVarChar).Value = Cart.ShippingCity;
                Access.mySqlCommand.Parameters.Add("@Shipping_Notice", SqlDbType.NVarChar).Value = Cart.OrderNotice;
                
                Result =Convert.ToInt16( Access.mySqlCommand.ExecuteScalar());
                Access.ClosedAccess();
                if (Result > 0) return Result;
                else return -1;
              
            //}
            //catch
            //{
            //    Access.ClosedAccess();
            //    return -1;

            //}
        }
       
	    #endregion
    }
}
